package com.im.service.noticeManage.smo;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.im.service.dto.BaseException;
import com.im.service.dto.NoticeManage;
import com.im.service.dto.PageQuery;
import com.im.service.noticeManage.dao.INoticeManageQueryDao;
import com.im.util.JsonUtil;
import com.im.util.ReturnsUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NoticeManageQuerySmoImpl implements INoticeManageQuerySmo {

    private static final Logger logger = LogManager.getLogger(NoticeManageQuerySmoImpl.class);

    @Autowired
    private INoticeManageQueryDao noticeManageQueryDao;

    @Override
    public List<NoticeManage> queryNoticeManageList( Map<String,Object> reqMap) throws Exception  {

        try {
            Map<String,Object> param = new HashMap<String,Object>();
            NoticeManage noticeManage = new NoticeManage();
            noticeManage.setResultId(String.valueOf(reqMap.get("resultId")));
            param.put("noticeManage", noticeManage);
            List<NoticeManage> list=noticeManageQueryDao.queryNoticeManageList(param);
            return list;
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            throw new Exception(ReturnsUtil.getStackTrackMessage(e));
        }
    }
    @Override
    public PageQuery<NoticeManage> queryNoticeManageList(String inJson, Map<String,Object> reqMap) throws Exception  {

        try {
            if (null!=inJson &&!JsonUtil.jsonValidate(inJson)) {
                throw new BaseException(
                        new StringBuffer().append("字符串： ").append(inJson).append("不符合JSON格式").toString());
            }
            PageQuery<NoticeManage> page = JsonUtil.jsonToBean(inJson, PageQuery.class);
            NoticeManage noticeManage = JsonUtil.jsonToBean(inJson, NoticeManage.class);

            Map<String,Object> param = new HashMap<String,Object>();
            param.put("noticeManage", noticeManage);
            param.put("page", page);

            PageHelper.startPage(page.getPageNumber(), page.getPageSize());
            List<NoticeManage> list=noticeManageQueryDao.queryNoticeManageList(param);
            page.setRows(list);
            page.setTotalPages(((Page<NoticeManage>)list).getPages());
            page.setTotal(((Page<NoticeManage>) list).getTotal());
            return page;
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            throw new Exception(ReturnsUtil.getStackTrackMessage(e));
        }
    }

    @Override
    public PageQuery<NoticeManage> queryNoticeManageListRelated(String inJson, Map<String,Object> reqMap) throws Exception{
        try {
            PageQuery<NoticeManage> page = JsonUtil.jsonToBean(inJson, PageQuery.class);
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("page", page);


            //特殊参数处理
            if(null!=reqMap){
                if(null!=reqMap.get("orgId")){
                    param.put("orgId",reqMap.get("orgId"));
                }
                if(null!=reqMap.get("planProjClass")){
                    param.put("planProjClass",reqMap.get("planProjClass"));
                }
                if(null!=reqMap.get("impleProjId")){
                    param.put("impleProjId",reqMap.get("impleProjId"));
                }

            }

            PageHelper.startPage(page.getPageNumber(), page.getPageSize());
            List<NoticeManage> list=noticeManageQueryDao.queryNoticeManageListRelated(param);
            page.setRows(list);
            page.setTotalPages(((Page<NoticeManage>)list).getPages());
            page.setTotal(((Page<NoticeManage>) list).getTotal());
            return page;
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            throw new Exception(ReturnsUtil.getStackTrackMessage(e));
        }
    }

    @Override
    public NoticeManage queryNoticeManageDetail(NoticeManage req) throws Exception{

        try {
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("noticeManage", req);
            NoticeManage noticeManageDetail = noticeManageQueryDao.queryNoticeManageDetail(param);
            return noticeManageDetail;
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            throw new Exception(ReturnsUtil.getStackTrackMessage(e));
        }
    }

    public NoticeManage queryNoticeManageById(String  noticeManageId) throws Exception{

        try {
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("noticeManageId", noticeManageId);
            NoticeManage noticeManage = noticeManageQueryDao.queryNoticeManageById(param);
            return noticeManage;
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            throw new Exception(ReturnsUtil.getStackTrackMessage(e));
        }
    }
}
